package com.ks.core.api.config;


import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author kingdee
 */
@Component
@Data
public class ApiConfig {

    /**
     * 版本号
     */
    public static final String VERSION = "1.0";

    /**
     * 请求格式
     */
    public static final String FORMAT = "json";

    /**
     * 加密方式
     */
    public static final String SIGN_METHOD = "MD5";

    /**
     * 拒绝请求分钟
     */
    @Value("${api-config.time-expired:#{T(com.ks.core.api.config.ApiConfig).TIME_EXPIRED}}")
    private long timeExpired;

    public static final long TIME_EXPIRED = 20;

    /**
     * 接口频率/s
     */
    @Value("${api-config.default-call-rate:#{T(com.ks.core.api.config.ApiConfig).DEFAULT_CALL_RATE}}")
    private double defaultCallRate;

    public static final double DEFAULT_CALL_RATE = 10;

    /**
     * 获取令牌超时间
     */
    @Value("${api-config.time-out-mill:#{T(com.ks.core.api.config.ApiConfig).TIME_OUT_MILL}}")
    private long timeOutMill;

    public static final long TIME_OUT_MILL = 100;
    /**
     * 批量操作时数据条数
     */
    @Value("${api-config.batch-limit-count:#{T(com.ks.core.api.config.ApiConfig).BATCH_LIMIT_COUNT}}")
    private long batchLimitCount;

    public static final long BATCH_LIMIT_COUNT = 300;

    public static final String SIGN_FIELD = "sign";

    public static final String APP_SECRET_FIELD = "appSecret";

    /**
     * 校验 {@link Value} 注入的属性
     */
    @PostConstruct
    public void validateProperties() {
        if (timeExpired <= 0) {
            timeExpired = TIME_EXPIRED;
        }
        if (defaultCallRate <= 0) {
            defaultCallRate = DEFAULT_CALL_RATE;
        }
        if (timeOutMill <= 0) {
            timeOutMill = TIME_OUT_MILL;
        }
        if (batchLimitCount <= 0) {
            batchLimitCount = BATCH_LIMIT_COUNT;
        }
    }
}
